/* Copyright (c) 2007 John Wells

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameLevel.Serialization
{
    public class LevelReader : ContentTypeReader<Level>
    {
        protected override Level Read(ContentReader input, Level existingInstance)
        {
            // Use the built-in types to read back the BrushVertex buffer
            //VertexBuffer brushVertexBuffer = null;
            //if (input.ReadBoolean())
            //    brushVertexBuffer = input.ReadObject<VertexBuffer>();

            // Create a level object using the graphics device - baking it into the loaded type
            Level output = new Level(input.ContentManager);

            // Read CSP geometry converted to models
            output.Geometry = input.ReadObject<Model[]>();

            // Brush geometry which should be treated as models
            output.GeometryModels = input.ReadObject<GeometryModel[]>();

            output.Terrains = input.ReadObject<Terrain[]>();
            // Primitives are the geometry data that tie vertices into walls, etc
            //output.Primitives = input.ReadObject<Primitive[]>();
            
            // Entities are just key/value pairs of strings
            output.Entities = input.ReadObject<Level.Entity[]>();

            output.Models = input.ReadObject<LevelModel[]>();
            output.ModelCmds = input.ReadObject<ModelCommand[]>();

            output.Triggers = input.ReadObject<LevelTrigger[]>();

            output.CollisionBoxes = input.ReadObject<CollisionBox[]>();

            // Lightmap for the brushes
            output.Lightmaps = input.ReadObject<Texture2D[]>();

            // Lightmap for the brushes
            output.LightDirectionMaps = input.ReadObject<Texture2D[]>();

            // Initialize the recently loaded objects

            /*
            foreach (Primitive item in output.Primitives)
            {
                item.LoadContent(input.ContentManager, output.Lightmaps);
                
            }
            */

            

            return output;
        } 
    }
}
